<template>
  <el-dialog width="600px" :title="dialogTitle" :visible.sync="dialogVisible" @close="handleClose">
    <el-form
      ref="submitForm"
      v-loading="formLoading"
      :model="submitForm"
      :rules="submitFormRules"
      label-width="80px"
      label-position="right"
      class="submit-form"
    >
      <el-form-item label="班次名称" prop="name">
        <el-input v-model="submitForm.name" placeholder="请输入班次名称" />
      </el-form-item>
      <el-form-item label="开始时间" prop="startTime">
        <el-time-select
          v-model="submitForm.startTime"
          style="width: 100%;"
          :picker-options="{
            start: '01:30',
            step: '01:30',
            end: '19:30'
          }"
          placeholder="选择开始时间"
        />
      </el-form-item>
      <el-form-item label="结束时间" prop="endTime">
        <el-time-select
          v-model="submitForm.endTime"
          style="width: 100%;"
          :picker-options="{
            start: '01:30',
            step: '1:30',
            end: '24:00'
          }"
          placeholder="选择结束时间"
        />
      </el-form-item>
      <el-form-item label="状态" prop="state">
        <el-switch
          v-model="submitForm.state"
          active-color="#13ce66"
          inactive-color="#ff4949"
          active-text="可用"
          inactive-text="禁用"
          active-value="1"
          inactive-value="0"
        />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="submitForm.remark" type="textarea" placeholder="请输入备注" :autosize="{minRows:2}" />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button :loading="formLoading" type="text" @click="handleClose">取消</el-button>
      <el-button :loading="formLoading" type="primary" @click="handleSubmit">提交</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { updateShiftById, saveShift, selectShiftById } from '@/api/erp/shift'

/**
 * @author Administrator
 * @date 2023/5/7 19:05
 **/
export default {
  name: 'SubmitModule',
  props: {
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      dialogVisible: this.visible,
      dialogTitle: '',
      formLoading: false,
      submitForm: {
        id: '',
        name: '',
        startTime: '07:30',
        endTime: '19:30',
        state: '1'
      },
      submitFormRules: {
        name: [{ required: true, message: '班次名称不能为空', trigger: 'change' }],
        startTime: [{ required: true, message: '开始时间不能为空', trigger: 'change' }],
        endTime: [{ required: true, message: '结束时间不能为空', trigger: 'change' }],
        state: [{ required: true, message: '状态', trigger: 'change' }]
      }
    }
  },
  methods: {
    init(id) {
      if (!id) {
        this.dialogTitle = '新增班次'
        return
      }
      this.dialogTitle = '修改班次'
      this.formLoading = true
      selectShiftById(id).then(resp => {
        if (resp.success) {
          this.submitForm = resp.data
        }
      }).finally(() => {
        this.formLoading = false
      })
    },
    handleSubmit() {
      this.$refs.submitForm.validate((valid) => {
        if (valid) {
          let request = saveShift
          if (this.submitForm.id) {
            request = updateShiftById
          }
          this.formLoading = true
          request(this.submitForm).then(resp => {
            if (resp.success) {
              this.$message.success('操作成功')
              this.$refs.submitForm.resetFields()
              this.handleClose()
            }
          }).finally(() => {
            this.formLoading = false
          })
        }
      })
    },
    handleClose() {
      this.$emit('close')
    }
  }
}
</script>

<style scoped>

</style>
